package com.supr.service;

import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import com.supr.mapper.CategoryMapper;
import com.supr.model.Category;
import com.supr.util.Pager;
import com.supr.util.cache.EhcacheUtil;

@Service
@Transactional(isolation = Isolation.DEFAULT, rollbackFor = Exception.class)
public class CategoryService {

	@Autowired
	private CategoryMapper categoryMapper;

	public List<Category> getCategoryList() {
		List<Category> categoryList = (List<Category>) EhcacheUtil.get("defaultCache", "categoryList");
		if (categoryList == null) {
			categoryList = categoryMapper.getAllCategoryList();
			EhcacheUtil.put("defaultCache", "categoryList", categoryList);
		}
		return categoryList;
	}

	public Category getCategoryByAlias(String aliasName) {
		return categoryMapper.getCategoryByAlias(aliasName);
	}

	public List<Category> getCategoryList(HashMap<String, Object> paramMap, Pager<Category> pager) {
		// TODO Auto-generated method stub
		List<Category> categoryList = null;
		int count = categoryMapper.getCategoryCount(paramMap);
		pager.setTotalPageNum(count);
		if(count > 0){
			paramMap.put("start", pager.getStart());
			paramMap.put("limit", pager.getLimit());
			categoryList = categoryMapper.getCategoryList(paramMap);
		}
		return categoryList;
	}

	public int addCategory(Category category) {
		// TODO Auto-generated method stub
		return categoryMapper.addCategory(category);
	}

	public Category getCategoryById(String id) {
		// TODO Auto-generated method stub
		return categoryMapper.getCategoryById(id);
	}

	public int editCategory(Category category) {
		// TODO Auto-generated method stub
		return categoryMapper.editCategory(category);
	}
	

}
